<?php

class Match{

	public $leaverGrenze = 6;
	const submissionForCancel = 2;

	function getMatchData($matchID){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();
		$ret['debug'] .= "Start getMatchData <br>\n";
		if($matchID > 0){
			$sql="SELECT m.*, mm.Name as MatchMode, mt.Name as MatchType, mm.Shortcut, r.Name as Region, r.Shortcut as RegionShortcut
					FROM `Match` m JOIN MatchMode mm ON mm.MatchModeID = m.MatchModeID
					JOIN MatchType mt ON mt.MatchTypeID = m.MatchTypeID
					JOIN Region r ON m.Region = r.RegionID
					WHERE MatchID = ".(int) $matchID."
							";
			$data = $DB->select($sql);
			$ret['data'] =$data;
			$ret['status'] = true;
		}
		else{
			$ret['status'] = "MatchID = 0";
		}

		$ret['debug'] .= "End getMatchData <br>\n";

		return $ret;

	}

	function acceptMatch($matchID){
		$ret = array();
		$matchID = (int) $matchID;
		$DB = new DB();
		$con = $DB->conDB();
		$Queue = new Queue();
		$steamID = $_SESSION['user']["steamID"];

		$log = new KLogger ( "log.txt" , KLogger::INFO );

		if($steamID > 0 && $matchID > 0){
			$sql = "UPDATE MatchTeams
					SET Ready = 1
					WHERE SteamID = ".secureNumber($steamID)." AND MatchID = ".(int)$matchID."
							";
			$DB->update($sql,0);
			$updateSuccess = mysql_affected_rows();
			if($updateSuccess == 0){
				$sql = "SELECT *
						FROM `MatchTeams`
						WHERE SteamID = ".secureNumber($steamID)." AND MatchID = ".(int)$matchID."
								";
				$data = $DB->multiSelect($sql);

				$log->LogInfo("update MatchTeams beim accepten fehlgeschlagen - SteamID:".$steamID." MatchID:".$matchID." Daten in MatchTeams �ber spieler: ".print_r($data,1)." BrowserDaten:".print_r(get_browser(null, true),1));	//Prints to the log file
			}
			else{
				$log->LogInfo("update MatchTeams OK - SteamID:".$steamID." MatchID:".$matchID);	//Prints to the log file
			}

			//$ret['sql'] = $sql;
			// Aus Queue entfernen, da schon n schritt weiter
			//$Queue->leaveQueue();

			$ret['status'] = true;
		}
		else{
			$log->LogInfo("accept Match fehlgeschlagen - SteamID:".$steamID." MatchID:".$matchID);	//Prints to the log file
			$ret['status'] = false;
		}

		return $ret;
	}


	function declineMatch($matchID){
		$ret = array();
		$matchID = (int) $matchID;
		$DB = new DB();
		$con = $DB->conDB();
		$steamID = $_SESSION['user']["steamID"];
		if($steamID > 0){
			$sql = "UPDATE MatchTeams
					SET Ready = 0
					WHERE SteamID = ".$steamID." AND MatchID = ".$matchID."
							";
			$DB->update($sql,0);
			$ret['status'] = true;
		}
		else{
			$ret['status'] = "not logged in!";
		}
		return $ret;
	}

	function checkAllReadyForMatch($matchID){
		$ret = array();
		$matchID = (int) $matchID;
		$DB = new DB();
		$con = $DB->conDB();
		$steamID = $_SESSION['user']["steamID"];
		if($steamID > 0){
			$sql = "SELECT mt.*, u.Name as Name, u.Avatar as Avatar
					FROM MatchTeams mt JOIN User u ON u.SteamID = mt.SteamID
					WHERE MatchID = ".(int)$matchID." -- AND Ready = 1
							LIMIT 10
							";
			$data = $DB->multiSelect($sql);
			
			// Count berechnen
			if(is_array($data) && count($data) > 0){
				$count = 0;
				foreach($data as $k =>$v){
					if($v['Ready'] == 1){
						$count++;
					}
				}
			}
			
			$ret['status'] = true;
			$ret['data'] = $data;
			$ret['countReady'] = $count;
		}
		else{
			$ret['status'] = "not logged in!";
		}

		return $ret;
	}

	function getReadyMatchData($matchID){
		$ret = array();
		$matchID = (int) $matchID;
		$DB = new DB();
		$con = $DB->conDB();

		$matchID = (int) $matchID;
		if($matchID > 0){
			// zuerst auslesen ob cancel Aufträge existieren gruppiert nach MatchID
			$sql = "SELECT COUNT(VoteForPlayer) as Count, VoteForPlayer
					FROM `MatchDetailsCancelMatchVotes`
					WHERE Reason = 1 AND MatchID = ".(int)$matchID."
							GROUP BY MatchID, VoteForPlayer
							HAVING Count >= ".(int)RELEVANTCANCELCOUNT."
									";

			$countLeaver = $DB->countRows($sql);
			$ret['debug'] .= p($sql,1);
			$ret['debug'] .= p($countLeaver,1);

			// Prüfen ob genügend votes eingegangen sind
			// Count berechnen anhand der potentiellen Leaver
			$tmpCount = (int) floor((10 - $countLeaver) / 100 * MATCHCANCELBORDER);

			$sql = "SELECT u.SteamID, md.Elo, us.Lvl, us.Wins, u.Name, u.Avatar, u.ProfileURL, t.Name as Team, md.TeamID, md.EloChange, IF(COUNT(mdlv.VoteForPlayer) >= 6 OR COUNT(mdcmv.VoteForPlayer) >= ".(int)$tmpCount.",1,0) as Leaver, SUM(uc.Vote) as Credits
					FROM MatchDetails as md
					LEFT JOIN UserCredits uc ON md.SteamID = uc.SteamID
					LEFT JOIN MatchDetailsLeaverVotes as mdlv ON mdlv.VoteForPlayer = md.SteamID AND mdlv.MatchID = md.MatchID
					LEFT JOIN MatchDetailsCancelMatchVotes as mdcmv ON mdcmv.VoteForPlayer = md.SteamID AND mdcmv.MatchID = md.MatchID,
					User as u, UserStats as us, Team as t
					WHERE (md.SteamID = u.SteamID AND u.SteamID = us.SteamID AND md.TeamID = t.TeamID)
					AND
					md.MatchID = ".(int)$matchID."
							GROUP BY md.SteamID
							LIMIT 10
							";

			$data = $DB->multiSelect($sql);
			//$ret['test'] = $data;

			// Host f�r Match auslesen
			$MatchDetailsHostLobby = new MatchDetailsHostLobby();
			$hostRet = $MatchDetailsHostLobby->getHostForMatch($matchID);
			$hostSteamID = $hostRet['data'];

			// data aufbereiten, Player in richtige Unterarrays aufteilen team1 und team2
			if(is_array($data) && count($data)>0){
				$ret['data']['team1'] = array();
				$ret['data']['team2'] = array();

				foreach($data as $k => $v){
					if($v['TeamID'] == 1){
						array_push($ret['data']['team1'],$v);
					}
					else{
						array_push($ret['data']['team2'],$v);
					}
				}

				// Matchdetails berechnen, wie duruchschnitts-Elo von team1 ect
				$ret['matchdetails'] = array();

				$ret['matchdetails']['host'] = $hostSteamID;

				// für team1 und team2
				for($i=1; $i<=2; $i++){
					$elo_sum = 0;
					foreach($ret['data']['team'.$i.''] as $k => $v){
						$elo_sum += $v['Elo'];
					}
					$rank_team = (int) $elo_sum / 5;
					$ret['matchdetails']['ave_rank_team'.$i.''] = round($rank_team,0);
				}

				$ret = $this->calculateAllWinLoseElos($ret);

				// Teams nach Elo sortieren
				$ret['data']['team1'] = orderArrayBy($ret['data']['team1'],'Elo',SORT_DESC);
				$ret['data']['team2'] = orderArrayBy($ret['data']['team2'],'Elo',SORT_DESC);

				$ret['status'] = true;
			}
			else{
				$ret['status'] = false;
				$ret['sql'] = $sql;
			}

		}
		else{
			$ret['status'] = "MatchID is wrong!";
		}

		return $ret;
	}

	function isPlayerInMatch($matchID=0, $steamID=0){
		$matchID = (int) $matchID;

		$DB = new DB();
		$con = $DB->conDB();
		$ret = false;

		if($steamID == 0){
			$steamID = $_SESSION['user']["steamID"];
		}

		if($steamID > 0){
			if($matchID > 0){

				$sql = "SELECT SteamID
						FROM MatchDetails
						WHERE MatchID = ".(int)$matchID." AND SteamID = ".secureNumber($steamID)."
								LIMIT 1
								";
				$count = $DB->countRows($sql,0);
				if($count == 1){
					$ret = true;
				}
				else{
					$ret = false;
				}
			}
			else{
				$sql = "SELECT m.MatchID
						FROM `Match` m JOIN `MatchDetails` md ON m.MatchID = md.MatchID
						WHERE md.SteamID = ".secureNumber($steamID)."
							 AND md.Submitted = 0 AND md.SubmissionFor = 0 AND SubmissionTimestamp = 0
								AND m.TeamWonID = -1 AND m.Canceled = 0 AND m.ManuallyCheck = 0 AND m.TimestampClosed = 0
								";
				$count = $DB->countRows($sql);

				if($count > 0){
					$ret = true;
				}
				else{
					$ret = false;
				}

			}
		}
		else{
			$ret = "not logged in!";
		}

		return $ret;
	}

	function deleteCreatedMatch($matchID){
		$DB = new DB();
		$con = $DB->conDB();
		$ret = array();
		$matchID = (int) $matchID;

		$log = new KLogger ( "log.txt" , KLogger::INFO );
		$log->LogInfo("Clean created Match(".$matchID.")! initiated by:".$_SESSION['user']["steamID"]);	//Prints to the log file


		$sql = "DELETE FROM `Match`
				WHERE MatchID = ".$matchID."
						";
		$DB->delete($sql);
		$ret['status'] = true;
		return $ret;
	}

	function calculateWinLoseEloForPlayer($rank, $rankOther){
		$ret = array();

		// Win-Elo
		$w = 1; // Win= 100%
		$c1 = 50; // Anfangskonstante
		$c2 = 400; // andere ominöse Konstante
		$eloDiff = $rank-$rankOther;
		$eTemp = $eloDiff/$c2 * (-1);
		$eNenner = 1 + pow(10,$eTemp);
		$e = 1/$eNenner;
		$winElo = (int) $c1 * ($w - $e);
		$ret['debug_elo_calc'] = "eDiff:".$eloDiff." eTemp:".$eTemp." eNenner:".$eNenner." e:".$e." winElo:".$winElo;
		$ret['WinElo'] = "+".round($winElo,0);

		// LoseElo
		$w = 0; // Lose = 0%
		$loseElo = (int) $c1 * ($w - $e);
		$ret['LoseElo'] = round($loseElo,0);

		return $ret;
	}

	function calculateAllWinLoseElos($array){
		$ret = array();

		for($i=1; $i<=2; $i++){
			if($i == 1){
				$rankOther = $array['matchdetails']['ave_rank_team2'];
			}
			else{
				$rankOther = $array['matchdetails']['ave_rank_team1'];
			}
			// Win/Lose Elo berechnen
			foreach($array['data']['team'.$i.''] as $k => $v){
				$tmp_array = array_merge($array['data']['team'.$i.''][$k], $this->calculateWinLoseEloForPlayer($v['Elo'], $rankOther));
				$array['data']['team'.$i.''][$k] = $tmp_array;
			}
		}
		//$array['test_elo'] = $this->calculateWinLoseEloForPlayer(1400, 1400);
		$ret = $array;
		return $ret;
	}

	function submitResult($selection, $matchID, $screenshot, $leaver){
		$DB = new DB();
		$con = $DB->conDB();
		$ret = array();
		$matchID = (int) $matchID;
		$steamID = $_SESSION['user']["steamID"];
		$ret['debug'] .= "selection: ".$selection;
		$ret['debug'] .= "MatchID: ".$matchID." - ".$steamID."\n";
		if($steamID > 0){
			if($matchID > 0){
				$MatchDetails = new MatchDetails();

				$ret['debug'] .= "Start getMatchDataOfPlayer\n";
				$matchData =  $this->getMatchDataOfPlayer($matchID);
				$ret['debug'] .= $matchData["debug"];
				$ret['debug'] .= "End getMatchDataOfPlayer\n";
				$matchModeID = $matchData['data']['MatchModeID'];
				$matchTypeID = $matchData['data']['MatchTypeID'];

				$ret['debug'] .= "Start getReadyMatchData\n";
				//$matchDetailsData = $this->getReadyMatchData($matchID);
				$ret['debug'] .= "End getReadyMatchData\n";

				switch($selection){
					case "won":
						//$eloChangeValue = $eloChange['WinElo'];
						$submissionFor = MatchDetails::winValue;
						break;
					case "lost":
						//$eloChangeValue = $eloChange['LoseElo'];
						$submissionFor = MatchDetails::loseValue;
						break;
				}
				// Screenshot handling
				if($screenshot != ""){
					$screenshotUploaded = 1;
				}
				else{
					$screenshotUploaded = 0;
				}

				// MatchDetails updaten
				$sql = "UPDATE MatchDetails
						SET Submitted = 1, ScreenshotUploaded = ".$screenshotUploaded.", SubmissionFor = ".$submissionFor.", SubmissionTimestamp = ".time()."
								WHERE MatchID = ".(int) $matchID." AND SteamID = ".secureNumber($steamID)."
										";

				$ret['debug'] .= $sql;

				$DB->update($sql);

				// MatchDetailsLEaverVotes updaten
				$MatchDetailsLeaverVotes = new MatchDetailsLeaverVotes();
				$insertLeaverDebug = $MatchDetailsLeaverVotes->insertSelectedLeaver($matchID, $leaver, $steamID);
				$ret['debug'] .= $insertLeaverDebug['debug'];

				// Match Lock rausnehmen
				$_SESSION['matchLock'] = false;

				$ret['status'] = true;
			}
		}
		else{
			$ret['status'] = "not logged in!";
		}

		return $ret;
	}

	function saveMatchDetails($matchID){
		$DB = new DB();
		$con = $DB->conDB();
		$ret = array();
		$matchID = (int) $matchID;
		$steamID = $_SESSION['user']["steamID"];
		//$Matchmaking = new Matchmaking();

		if($matchID > 0){

			// check if already exisits
			$sql = "SELECT MatchID
					FROM `MatchDetails`
					WHERE MatchID = ".(int) $matchID."
							";
			$count = $DB->countRows($sql,0);

			$ret['debug'] .= $sql."\n";
			// wenn noch nciht eingetragen, dann eintragen und MatchTeams löschen
			if($count == 0){
				$log = new KLogger ( "log.txt" , KLogger::INFO );
				$log->LogInfo("MatchDetails werden eingetragen: MatchID:".$matchID." SteamID:".$steamID);	//Prints to the log file

				$sql = "SELECT MatchID, SteamID, TeamID, Rank
						FROM `MatchTeams`
						WHERE MatchID = ".(int) $matchID."
								";
				$data = $DB->multiSelect($sql);
				$log->LogInfo("Data in MatchTeams zu MatchID:".$matchID." SteamID:".$steamID." ".print_r($data,1));	//Prints to the log file

				$ret['debug'] .= print_r($data,1)."\n";

				if(is_array($data) && count($data) > 0){
					$values = "";
					foreach($data as $k => $v){
						$tmpArray = array();

						if($values != ""){
							$values .= ",";
						}
						$tmpArray['MatchID'] = (int) $matchID;
						$tmpArray['SteamID'] = secureNumber($v['SteamID']);
						$tmpArray['TeamID'] = (int) $v['TeamID'];
						$tmpArray['TimeStamp'] = time();
						$tmpArray['Elo'] = (int) $v['Rank'];

						//$values .= "(NULL, ".(int)$matchID.", ".$v['SteamID'].", 0, ".$v['TeamID'].", 0, ".time().", 0, '', 0, 0, )";
						$insertArray[] = $tmpArray;
					}

					// 					$sql = "INSERT INTO `MatchDetails`
					// 							VALUES ".$values."
					// 									";
					$DB->multiInsert("MatchDetails", $insertArray);
					$DB->select($sql, 0);

					$ret['debug'] .= $sql."\n";

					$ret['status'] = true;
				}
			}
			else{
				$ret['debug'] .= "bereits eingetragen! \n";
				$ret['status'] = true;
			}
		}
		else{
			$ret['status'] = false;
		}
		return $ret;
	}

	// return MatchID
	function insertNewFoundMatch($regionID, $matchTypeID, $matchModeID){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();
			
		$insertArray["MatchID"] = NULL;
		$insertArray["TeamWonID"] = -1;
		$insertArray["MatchTypeID"] = $matchTypeID;
		$insertArray["MatchModeID"] = $matchModeID;
		$insertArray['TimestampCreated'] = time();
		$insertArray["Region"] = (int) $regionID;

		$DB->insert("Match",$insertArray);
		$matchID = mysql_insert_id();

		$log = new KLogger ( "log.txt" , KLogger::INFO );
		$log->LogInfo("Match created! MatchID:".$matchID." at ".time());	//Prints to the log file

			
		$ret['matchID'] = $matchID;
			
		return $ret;
	}

	function getMatchDataOfPlayer($matchID, $select = "*"){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();
		if($matchID > 0){
			$sql = "SELECT ".$select."
					FROM `Match`
					WHERE MatchID = ".(int) $matchID."
							";
			$ret['debug'] = $sql;
			$ret['data'] = $DB->select($sql);
			$ret['status'] = true;
		}
		else{
			$ret['status'] = "matchID = 0";
		}

		return $ret;

	}




	function checkSubmissions($matchID){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();


		if($matchID > 0){
			$MatchDetails = new MatchDetails();

			// wieviele leute in % schon abgestimmt
			$alreadySubmittedPercentage = $MatchDetails->getPercentageOfAlreadySubmittedResults($matchID);

			// schon über 60% Stimme abgegeben?
			if($alreadySubmittedPercentage > 60){
				$strangeSubmissions = $MatchDetails->checkForStrangeSubmissions($matchID);
			}
			else{
					
			}
			$ret['status'] = true;
		}
		else{
			$ret['status'] = "matchID = 0";
		}
			
		return $ret;
	}

	function matchSubmitted($matchID){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();


		if($matchID > 0){
			$sql = "SELECT TeamWonID, TimestampClosed, Canceled
					FROM `Match`
					WHERE MatchID = ".(int) $matchID."";
			$ret['debug'] = $sql;

			$data = $DB->select($sql);

			if($data['TeamWonID'] > 0 && $data['TimestampClosed'] > 0){
				$ret['submitted'] = true;
			}
			elseif($data['Canceled'] == 1){
				$ret['submitted'] = true;
			}
			else{
				$ret['submitted'] = false;
			}
			$ret['status'] = true;
		}
		else{
			$ret['status'] = "matchID = 0";
		}
			
		return $ret;
	}

	function getTeamWonIDOfMatch($matchID){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();
		$ret['debug'] .= "Start getTeamWonIDOfMatch <br>\n";
		if($matchID > 0){
			$sql = "SELECT TeamWonID
					FROM `Match`
					WHERE MatchID = ".(int) $matchID."
							";
			$data = $DB->select($sql);

			$ret['TeamWonID'] = $data['TeamWonID'];

			$ret['status'] = true;
		}
		else{
			$ret['status'] = "MatchID = 0";
		}

		$ret['debug'] .= "End getTeamWonIDOfMatch <br>\n";

		return $ret;
	}

	function getOpenMatches($steamID){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();

		if($steamID > 0){


			$sql="SELECT m.MatchID, m.TimestampCreated, m.MatchTypeID, mm.Name as MatchMode, mm.Shortcut as MatchModeShortcut, md.Submitted
					FROM `Match` m JOIN `MatchDetails` md ON m.MatchID = md.MatchID
					JOIN MatchMode mm ON m.MatchModeID = mm.MatchModeID
					WHERE SteamID = ".secureNumber($steamID)." AND ((m.TeamWonID = -1 AND Canceled = 0 ) OR md.Submitted = 0)
							";

			$ret['debug'] .= $sql;
			$data = $DB->multiSelect($sql);

			if(is_array($data) && count($data) > 0){
				$tmpData2 = array();
				foreach($data as $k => $v){
					$tmpData = array();
					if($where != ""){
						$where .= " OR ";
					}
					$where .= "MatchID = ".(int) $v['MatchID'];

					$tmpData['MatchID'] = $v['MatchID'];
					$tmpData['MatchMode'] = $v['MatchMode'];
					$tmpData['MatchModeShortcut'] = $v['MatchModeShortcut'];
					$tmpData['MatchTypeID'] = $v['MatchTypeID'];
					$tmpData['TimestampCreated'] = $v['TimestampCreated'];
					$tmpData['Submitted'] = $v['Submitted'];
					$tmpData2[] = $tmpData;
				}
			}
			//$ret['debug'] .= p($tmpData2,1);

			$sql= "SELECT MatchID, COUNT(Submitted) AS SubmittedCount
					FROM MatchDetails
					WHERE (".$where.") AND Submitted = 1
							GROUP BY MatchID
							";
			$ret['debug'] .= $sql;
			$data2 = $DB->multiSelect($sql);

			// 			if(is_array($tmpData2) && count($tmpData2) > 0){
			// 				foreach($tmpData2 as $k => $v){
			// 					if(is_array($data2) && count($data2) > 0){
			// 						foreach($data2 as $kk => $vv){
			// 							if($v['MatchID'] == $vv['MatchID']){

			// 								$tmp['MatchID'] = $v['MatchID'];
			// 								$tmp['MatchMode'] = $v['MatchMode'];
			// 								$tmp['MatchModeShortcut'] = $v['MatchModeShortcut'];
			// 								$tmp['MatchTypeID'] = $v['MatchTypeID'];
			// 								$tmp['TimestampCreated'] = $v['TimestampCreated'];
			// 								$tmp['Submitted'] = $v['Submitted'];

			// 								$tmp['SubmittedCount'] = $vv['SubmittedCount'];

			// 								$retData[] = $tmp;
			// 							}
			// 						}
			// 					}
			// 				}
			// 			}
			if(is_array($data2) && count($data2) > 0){
				foreach($data2 as $kk => $vv){

					$key = recursive_array_search($vv['MatchID'], $tmpData2);
					$ret['debug'] .= p("KEY:".$key,1);
					$tmpData2[$key]['SubmittedCount'] = $vv['SubmittedCount'];
				}
			}

			$retData = $tmpData2;

			$ret['data'] = $retData;
			$ret['status'] = true;
		}
		else{
			$ret['status'] = "SteamID 0";
		}
		return $ret;
	}

	function getMatchesPlayedCount(){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();

		$sql="SELECT MatchID
				FROM `Match`
				WHERE TeamWonID != -1 AND TimestampClosed != 0
				";
		$count = $DB->countRows($sql);

		$ret['count'] = $count;
		$ret['status'] = true;

		return $ret;
	}

	function getCountOfLeaversInMatch($matchID){

		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();
		$ret['debug'] .= "Start getCountOfLeaversInMatch <br>\n";

		if($matchID > 0){
			$count = 0;

			$sql = "SELECT Count(VoteForPlayer) as Count, VoteForPlayer
					FROM `MatchDetailsLeaverVotes`
					WHERE MatchID = ".(int) $matchID."
							GROUP BY VoteForPlayer;
							";
			$data = $DB->multiSelect($sql);

			$count=0;

			if(is_array($data) && count($data) > 0){
				foreach($data as $k =>$v){
					if($v['Count'] >= $this->leaverGrenze){
						$count++;
					}
				}
			}

			$ret['count'] = $count;

			$ret['status'] = true;
		}
		else{
			$ret['status'] = "MatchID = 0";
		}

		$ret['debug'] .= "End getCountOfLeaversInMatch <br>\n";

		return $ret;

	}

	function playerLeftTheMatch($steamID, $matchID){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();
		$ret['debug'] .= "Start playerLeftTheMatch <br>\n";
		if($steamID > 0 && $matchID > 0){

			$sql = "SELECT Count(VoteForPlayer) as Count, VoteForPlayer
					FROM `MatchDetailsLeaverVotes`
					WHERE MatchID = ".(int) $matchID." AND VoteForPlayer = ".secureNumber($steamID)."
							GROUP BY VoteForPlayer;
							";
			$data = $DB->select($sql);

			if($data['Count'] >= $this->leaverGrenze){
				$MatchDetails = new MatchDetails();
				$data2 = $MatchDetails->getMatchDetailsDataOfPlayer($matchID, $steamID);
				$ret['data'] = $data2['data'];
				$ret['left'] = true;
			}
			else{
				$ret['left'] = false;
			}

			$ret['status'] = true;
		}
		else{
			$ret['status'] = "steamID = 0 or matchic = 0";
		}

		$ret['debug'] .= "End playerLeftTheMatch <br>\n";

		return $ret;
	}

	function getPlayersWhoLeftTheMatch($matchID){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();
		$ret['debug'] .= "Start getPlayersWhoLeftTheMatch <br>\n";
		if($matchID > 0){

			$sql = "SELECT Count(VoteForPlayer) as Count, VoteForPlayer
					FROM `MatchDetailsLeaverVotes`
					WHERE MatchID = ".(int) $matchID."
							GROUP BY VoteForPlayer;
							";
			$data = $DB->multiSelect($sql);
			if(is_array($data) && count($data) > 0){
				foreach($data as $k =>$v){
					if($v['Count'] >= $this->leaverGrenze){
						$MatchDetails = new MatchDetails();
						$data2 = $MatchDetails->getMatchDetailsDataOfPlayer($matchID, $v['VoteForPlayer']);
						$ret['data'][] = $data2['data'];
					}
				}
			}



			$ret['status'] = true;
		}
		else{
			$ret['status'] = "MatchID = 0";
		}

		$ret['debug'] .= "End getPlayersWhoLeftTheMatch <br>\n";

		return $ret;
	}

	function getHandicappedTeam($matchID){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();
		$ret['debug'] .= "Start insertNewUser <br>\n";
		if($matchID > 0){

			$data = $this->getPlayersWhoLeftTheMatch($matchID);
			$ret['debug'] .= p($data,1);
			$data = $data['data'];

			// bestimmten welches Team benachteiligt war
			$count = array();
			if(is_array($data) && count($data) > 0){
				foreach($data as $k =>$v){
					$ret['debug'] .= p($v,1);
					$count[$v['TeamID']]++;
				}
			}
			$ret['debug'] .= p($count,1);
			// gr��eren Wert nach vorne bringen
			arsort($count);
			$teamID = key($count);
			$ret['debug'] .= p($count,1);

			// wenn gr��erer wert minus kleinerer gr��er als 1 dann ein Team benachteiligt sonst fair

			switch($teamID){
				case 1:
					$otherTeam = 2;
					break;
				case 2:
					$otherTeam = 1;
					break;
			}

			$check = $count[$teamID] - $count[$otherTeam];

			$ret['debug'] .= "check danach: ".$check." \n";
			if($check > 0){
				$ret['teamID'] = $teamID;
				$ret['count'] = $count[$teamID];
			}
			else{
				$ret['teamID'] = false;
				$ret['count'] = 0;
			}

			$ret['status'] = true;
		}
		else{
			$ret['status'] = "MatchID = 0";
		}

		$ret['debug'] .= "End insertNewUser <br>\n";

		return $ret;
	}


	function cancelMatch($matchID, $voteArray, $reason){

		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();

		$steamID = $_SESSION['user']['steamID'];

		if($matchID > 0 && $steamID > 0){

			// Match erstma hard canceln (MatchDetails)
			$this->cancelMatchHard($matchID);

			if(is_array($voteArray) && count($voteArray) > 0){
				$insertArray = array();
				foreach ($voteArray as $k => $v) {
					$tmp['SteamID'] = secureNumber($steamID);
					$tmp['MatchID'] = (int)$matchID;
					$tmp['VoteForPlayer'] = secureNumber($v);
					$tmp['Timestamp'] = time();
					$tmp['Reason'] = (int) $reason;
					$ret['debug'] .= p($tmp,1);
					$DB->insert("MatchDetailsCancelMatchVotes", $tmp);
				}

				$ret['status'] = true;
			}
			else{
				$ret['status'] = false;
			}
		}
		else{
			$ret['status'] = "matchID = 0";
		}
		return $ret;

	}

	function playerVotedForCancelingMatch($steamID, $matchID){

		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();

		if($steamID > 0 && $matchID > 0){

			$sql = "SELECT *
					FROM `MatchDetails` md  LEFT JOIN `MatchDetailsCancelMatchVotes` mdcmv ON md.MatchID = mdcmv.MatchID AND md.SteamID = mdcmv.SteamID
					WHERE (mdcmv.SteamID = ".secureNumber($steamID)." AND mdcmv.MatchID = ".(int)$matchID.") OR
							(md.SubmissionFor = ".$this::submissionForCancel." AND md.MatchID = ".(int)$matchID." AND md.SteamID = ".secureNumber($steamID).")
									";
			$count = $DB->countRows($sql);
			$ret['debug'] .= p($sql,1);

			if ($count > 0) {
				$ret['canceled'] = true;
			}
			else {
				$ret['canceled'] = false;
			}

			$ret['status'] = true;
		}
		else{
			$ret['status'] = "SteamID 0 && matchID 0";
		}
		return $ret;

	}

	function matchAlreadyCanceled($matchID){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();
		$ret['debug'] .= "Start matchAlreadyCanceled <br>\n";

		if($matchID > 0){

			$sql = "SELECT *
					FROM `Match`
					WHERE Canceled = 1 AND MatchID = ".(int) $matchID."
							";
			$count = $DB->countRows($sql);
			$ret['debug'] .= p($sql,1);

			if($count > 0){
				$ret['canceled'] = true;
			}
			else{
				$ret['canceled'] = false;
			}

			$ret['status'] = true;
		}
		else{
			$ret['status'] = "MatchID = 0";
		}

		$ret['debug'] .= "End matchAlreadyCanceled <br>\n";

		return $ret;
	}


	function cancelMatchHard($matchID){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();
		$ret['debug'] .= "Start cancelMatchHard <br>\n";

		$steamID = $_SESSION['user']['steamID'];

		if($matchID > 0 && $steamID > 0){
			$MatchDetails = new MatchDetails();

			$cancelValue = (int) $MatchDetails::chancelValue;

			// in MatchDetails protokollieren
			$sql = "UPDATE `MatchDetails`
					SET Submitted = 1, SubmissionTimestamp = ".time().", SubmissionFor = ".$cancelValue."
							WHERE SteamID = ".secureNumber($steamID)." AND MatchID = ".(int) $matchID."
									";

			$DB->update($sql);

			// 			$sql="UPDATE `Match`
			// 			 		SET Canceled = 1
			// 					WHERE MatchID = ".(int) $matchID."
			// 					";
			// 			$DB->update($sql);

			$log = new KLogger ( "log.txt" , KLogger::INFO );
			$log->LogInfo("Match ".$matchID." canceled by ".$steamID."");	//Prints to the log file

			$ret['status'] = true;
		}
		else{
			$ret['status'] = "MatchID = 0";
		}

		$ret['debug'] .= "End cancelMatchHard <br>\n";

		return $ret;
	}

	function getMatchStateForUser($matchID, $steamID=0){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();

		if($steamID == 0){
			$steamID = $_SESSION['user']['steamID'];
		}

		$ret['debug'] .= "Start getMatchStateForUser <br>\n";
		if($matchID > 0 && $steamID>0){

			$retSubmitted = $this->matchSubmitted($matchID);
			$submitted = $retSubmitted['submitted'];

			$playerInMatch = $this->isPlayerInMatch($matchID, $steamID);

			$playerSubmitted = $this->gerPlayerSubmittedMatchResult($matchID, $steamID);
			$ret['debug'] .= p($playerSubmitted,1);
			$ret['data']['playerOfMatch'] = $playerInMatch;
			$ret['data']['MatchClosed'] = $submitted;
			$ret['data']['playerSubmitted'] = $playerSubmitted['submitted'];

			$ret['status'] = true;
		}
		else{
			$ret['status'] = "MatchID = 0";
		}

		$ret['debug'] .= "End getMatchStateForUser <br>\n";

		return $ret;
	}


	function gerPlayerSubmittedMatchResult($matchID, $steamID=0){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();
		$ret['debug'] .= "Start gerPlayerSubmittedMatchResult <br>\n";

		if($steamID == 0){
			$steamID = $_SESSION['user']['steamID'];
		}

		if($matchID > 0 && $steamID > 0){

			$playerInMatch = $this->isPlayerInMatch($matchID, $steamID);

			if($playerInMatch){
				$sql = "SELECT *
						FROM `MatchDetails`
						WHERE SteamID = ".secureNumber($steamID)." AND MatchID = ".(int) $matchID."
								AND Submitted != 0 AND SubmissionFor != 0 AND SubmissionTimestamp != 0
								";
				$ret['debug'] .= p($sql,1);
				$count = $DB->countRows($sql);
					
				if($count > 0){
					$ret['submitted'] = true;
				}
				else{
					$ret['submitted'] = false;
				}
			}
			else{
				$ret['submitted'] = true;
			}

			$ret['status'] = true;
		}
		else{
			$ret['status'] = "MatchID = 0";
		}

		$ret['debug'] .= "End gerPlayerSubmittedMatchResult <br>\n";

		return $ret;
	}

	function getLastPlayedMatches($matchModeID=0, $smarty=null, $limit=6){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();
		$ret['debug'] .= "Start getLastPlayedMatches <br>\n";

		if($matchModeID > 0){
			$where = "AND m.MatchModeID = ".(int)$matchModeID;
		}
		if($limit>0){
			$sql = "SELECT m.*, mm.Name as MatchModeName, mm.Shortcut as MatchModeShortcut
				FROM `Match` m JOIN MatchMode mm ON mm.MatchModeID = m.MatchModeID
				WHERE m.TeamWonID != -1 AND m.TimestampClosed != 0
				AND m.Canceled = 0 AND ManuallyCheck = 0 ".$where."
			
				ORDER BY m.MatchID DESC
				LIMIT ".(int)$limit."
				";
			$ret['debug'] .= p($sql,1);
			$data = $DB->multiSelect($sql);
			
			if($smarty != null){
				$smarty->assign('data', $data);
				$table = $smarty->fetch("index/loggedIn/wallOfFame/lastMatches/lastMatchesTable.tpl");
				$ret['data'] = $table;
			}
			else{
				$ret['data'] = $data;
			}
			
			$ret['status'] = true;
		}
		else{
			$ret['status'] = "limit = 0";
		}

		$ret['debug'] .= "End getLastPlayedMatches <br>\n";

		return $ret;
	}
}

?>